package com.garden.rocketmq.learn.demo.producer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

/**
 * 单向生产者 发送单向消息
 * 用于不关心发送结果的场景，如日志发送
 * Created by Garden on 2020-07-28 21:06
 */
public class OnewayProducer {

    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException {
        //实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("oneway_producer_group");
        //设置NameServer的地址，Producer启动后通过NameServer获取目标Topic的路由信息
        producer.setNamesrvAddr("localhost:9876");
        //启动Producer实例
        producer.start();

        //发送消息
        for (int i = 0; i < 100; i++){
            //创建消息，指定主题Topic，标签Tag，业务标识Key，消息体MsgBody [Tag可用于消息过滤，Key可用于消息查询]
            Message message = new Message("test_topic","oneway_producer_tag","oneway_producer_key." + i,("OnewayProducer Msg." + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            producer.sendOneway(message);
        }

        //发送完毕，关闭Producer实例
        producer.shutdown();
    }

}
